package com.hh.storeRoom.controller;

import com.hh.storeRoom.service.StoreRoomService;
import com.hh.storeRoom.vo.StoreRoomAddVo;
import com.hh.storeRoom.vo.StoreRoomUpdateStatusVo;
import com.hh.storeRoom.vo.StoreRoomUpdateVo;
import com.hh.storeRoom.vo.StoreRoomVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@CrossOrigin
@RestController
@RequestMapping("/storeRoom")
public class StoreRoomController {

    @Autowired
    private StoreRoomService storeRoomService;

    /**
     * 库房信息查询接口
     * @param storeRoomVo 接收前端查询条件
     * @return
     */
    @PostMapping("/queryPage")
    public ResultVo queryPage(@RequestBody StoreRoomVo storeRoomVo){
        return storeRoomService.queryPage(storeRoomVo);
    }

    /**
     * 库房添加接口
     * @param storeLogoAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public ResultVo add(@RequestBody @Valid StoreRoomAddVo storeLogoAddVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return storeRoomService.add(storeLogoAddVo);
    }

    /**
     * 库房更新接口
     * @param storeLogoUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    public ResultVo udapte(@RequestBody @Valid StoreRoomUpdateVo storeLogoUpdateVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return storeRoomService.update(storeLogoUpdateVo);
    }

    /**
     * 库房状态更新接口
     * @param storeRoomUpdateStatusVo
     * @return
     */
    @PostMapping("updateStatus")
    public ResultVo udapteStatus(@RequestBody StoreRoomUpdateStatusVo storeRoomUpdateStatusVo){
        return storeRoomService.udapteStatus(storeRoomUpdateStatusVo);
    }
}
